(function() {
    Nokia.Button = Nokia.RoundedCornerWidget.extend({
        initialize: function(options) {
            var instance = this;

			var defaults = {
				element: '',
				label: '&nbsp;',
				disabled: false,
				keeppressed: false,
				pressed: false,
				roundedCorners: true,
				width: 'auto',
				height: 'auto',
				click: function() {},
				disable: function() {},
				enable: function() {},
				press: function() {},
				unpress: function() {}
            };

            instance.options = jQuery.extend(defaults, options);

            instance.element = jQuery( instance.options.element );

            instance.label = instance.options.label;

            instance.disabled = instance.options.disabled;

            instance.pressed = instance.options.pressed;

            instance._super.apply(instance, [instance.options]);

            instance.registerData("button");

            instance.wrappers = [];

            instance._create();

            instance._attachEvents();

			instance.registerVibrationOn([]);

			instance.fireCallback('create');
        },

		_attachEvents: function() {
			var instance = this,
				options = instance.options,
				element = instance.element,
				wrapper = element.parent(),
				Styles = instance.Styles;

			var action = function(e) {

				if ( instance.disabled ) {
					return false;
				}

				Nokia.util.addClass(element[0], Styles.stateHighlight);
				Nokia.util.addBorderClass(wrapper, Styles.nokiaStateHighlight);

				// Simulate mouseup
				setTimeout(function() {
					Nokia.util.removeClass(element[0], Styles.stateHighlight);
					Nokia.util.removeBorderClass(wrapper, Styles.nokiaStateHighlight);
				}, 200);

				if ( options.keeppressed ) {
					if ( instance.pressed ) {
						instance.unpress();
					} else {
						instance.press();
					}
				}

				instance.fireCallback('click', e);

				return false;
			};

			wrapper.bind('click', action);

			Nokia.util.bindEnter(wrapper, action);

			wrapper.hover(function() {

				if ( instance.disabled ) {
					return;
				}

				Nokia.util.addBorderClass(wrapper, Styles.nokiaStateHover);
				Nokia.util.addClass(element[0], Styles.stateHover);

				if ( instance.pressed ) {
					Nokia.util.removeClass(element[0], Styles.stateActive);
					Nokia.util.removeBorderClass(wrapper, Styles.nokiaStateActive);
				}

				instance.fireCallback('focus');
			}, function() {

				Nokia.util.removeBorderClass(wrapper, Styles.nokiaStateHover);
				Nokia.util.removeClass(element[0], Styles.stateHover);

				if ( instance.pressed ) {
					Nokia.util.addClass(element[0], Styles.stateActive);
					Nokia.util.addBorderClass(wrapper, Styles.nokiaStateActive);
				}

				instance.fireCallback('unfocus');
			});

		},

		_clear: function() {
        	var instance = this;

        	instance.element.unbind('click');
        	instance.element.unbind('mouseup');
        	instance.element.unbind('mousedown');
        },

		_create: function() {
			var instance = this;
			var options = instance.options;
			var element = instance.element;
			var Styles = instance.Styles;
			var width = options.width;
			var height = options.height;

			jQuery.each(element, function(i, el) {
				var oldClass = el.className;
				var wrapper = Nokia.dom.parseHTML('<a href="javascript:void(0);" class="' + Styles.nokiaButton +'"></a>');

				Nokia.dom.wrap(wrapper, el);

				instance.wrappers.push( jQuery(el).parents('div:first') );

				el.className = [Styles.nokiaButtonElement, oldClass, Styles.stateDefault].join(' ');
				el.innerHTML = options.label;

				wrapper.width(width);
				element.height(height);

				if ( options.roundedCorners ) {
					Nokia.util.applyRoundedCorners(el);
				}

				if ( options.disabled ) {
					Nokia.util.addClass(el, Styles.stateDisabled);
					Nokia.util.addBorderClass(wrapper, Styles.nokiaStateDisabled);
				}

				if ( options.pressed ) {
					instance.press();
				}

				Nokia.util.disableSelection(el);
			});

		},

		_changeBorderColors: function(state, className) {
			var instance = this;
			var element = instance.element;
			var wrapper = element.parent();
			var Styles = instance.Styles;

			var top = wrapper.find('.nokia-border-top');
			var bottom = wrapper.find('.nokia-border-bottom');

			if ( state ) {
				Nokia.util.addClass(top[0], className);
				Nokia.util.addClass(bottom[0], className);
			} else {
				Nokia.util.removeClass(top[0], className);
				Nokia.util.removeClass(bottom[0], className);
			}
		},

		_setState: function(type, state) {
			var instance = this;
			var Styles = instance.Styles;
			var element = instance.element;
			var wrapper = instance.element.parent();
			var stateClass = type == 'press' ? Styles.stateActive : Styles.stateDisabled;
			var borderClass = type == 'press' ? Styles.nokiaStateActive : Styles.nokiaStateDisabled;

			jQuery.each(element, function(i, el) {
				if ( state ) {
					Nokia.util.addClass(el, stateClass);
					Nokia.util.addBorderClass(wrapper, borderClass);

					if ( type == 'press' ) {

						if (element.hasClass(Styles.stateHover)) {
							Nokia.util.removeClass(el, stateClass);
						}

						instance._changeBorderColors(state, Styles.nokiaStateActive);
					}
				} else {
					Nokia.util.removeClass(el, stateClass);
					Nokia.util.removeBorderClass(wrapper, borderClass);

					if ( type == 'press' ) {
						instance._changeBorderColors(state, Styles.nokiaStateActive);
					}
				}
			});
		},

		show: function() {
			var instance = this;

			instance.element.parent().show();
		},

		hide: function() {
			var instance = this;

			instance.element.parent().hide();
		},

		disable: function() {
			var instance = this;

			instance.disabled = true;
			instance._setState('disabled', instance.disabled);

			instance.fireCallback('disabled');
		},

		enable: function() {
			var instance = this;

			instance.disabled = false;
			instance._setState('disabled', instance.disabled);

			instance.fireCallback('enable');
		},

		getLabel: function() {
			var instance = this;
			var label = instance.label;

			return label;
		},

		press: function() {
			var instance = this;

			instance.pressed = true;
			instance._setState('press', instance.pressed);

			instance.fireCallback('press');
		},

		setBorderColor: function(color) {
			var instance = this;
			var wrapper = instance.element.parent();

			instance._super(wrapper, color);
		},

		setLabel: function(label) {
			var instance = this;
			var element = instance.element;

			instance.label = label;

			element.html(label);
		},

		unpress: function() {
			var instance = this;

			instance.pressed = false;
			instance._setState('press', instance.pressed);

			instance.fireCallback('unpress');
		},

		Styles: {
			nokiaButton: 'nokia-button',
			nokiaButtonElement: 'nokia-button-element',
			nokiaStateDefault: 'nokia-border-state-default',
			nokiaStateDisabled: 'nokia-border-state-disabled',
			nokiaStateActive: 'nokia-border-state-active',
			nokiaStateHighlight: 'nokia-border-state-highlight',
			nokiaStateHover: 'nokia-border-state-hover',
			stateActive: 'ui-state-active',
			stateDisabled: 'ui-state-disabled',
			stateDefault: 'ui-state-default',
			stateHighlight: 'ui-state-highlight',
			stateHover: 'ui-state-hover'
		}
    });
})();